﻿using AntMovement.Core;
using AntMovement.Core.IParameter;
using AntMovement.Core.Logic;
using AntMovement.Core.Model;
using AntMovement.Parameter;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace AntMovement.Menu.Device.ViewModels
{
    public class SingleControlConfingViewModel
    {
        public ObservableCollection<string> OutputNames { get; set; }
        public ObservableCollection<string> InputNames { get; set; }
        public IOParameter IOP { get; set; }
        private IDialogHost _dialogHost;
        public SingleControlConfingViewModel(IIOParameter iOParameter, IDialogHost dialogHost)
        {
            _dialogHost = dialogHost;
            IOP = (IOParameter)iOParameter;
            var outp = IOP.DODataSet.Where(x => (x.Text != null) && (x.Text.Contains("真空") || x.Text.Contains("大气") || x.Text.Contains("电机"))).Select(x => x.Text).ToList();
            var inp = IOP.DIDataSet.Where(x => (x.Text != null) && (x.Text.Contains("真空"))).Select(x => x.Text).ToList();
            OutputNames = new ObservableCollection<string>(outp);
            OutputNames.Add(string.Empty);
            InputNames = new ObservableCollection<string>(inp);
            InputNames.Add(string.Empty);
        }

        public DelegateCommand<string> AirCylinderCmd => new DelegateCommand<string>(sender =>
        {
            switch (sender)
            {
                case "添加":
                    IOP.SingleControlSource.Add(new SingleControlModel()
                    {
                        Index = IOP.SingleControlSource.Count + 1,
                        Type = "真空"
                    });
                    break;

                case "保存":
                    foreach (var item in IOP.SingleControlSource)
                    {
                        if (!string.IsNullOrEmpty(item.Out_Name))
                        {
                            var outText = IOP.DODataSet.FirstOrDefault(x => x.Text == item.Out_Name);
                            if (outText != null)
                            {
                                item.Out_Text = $"Y{outText.MdlIndex:D2}{outText.OrderlyNum:D2}";
                            }
                        }
                        if (item.Type == "真空")
                        {
                            if (string.IsNullOrEmpty(item.In_Name))
                            {
                                _dialogHost.DialogShow("真空控制没有选择输入信号!!!", Core.Enum.LogLevel.Warning, hostDialog: Core.Enum.PopUpType.Global);
                                return;
                            }
                            var inText = IOP.DIDataSet.FirstOrDefault(x => x.Text == item.In_Name);
                            if (inText != null)
                            {
                                item.In_Text = $"X{inText.CardNo:D2}{(inText.MdlIndex * 16) + inText.OrderlyNum:D2}";
                            }
                        }
                    }
                    IOP.Save();
                    _dialogHost.DialogShow("保存成功!!!", Core.Enum.LogLevel.Success, hostDialog: Core.Enum.PopUpType.Global);
                    break;
            }
        });

        private Action<SingleControlModel, SingleControlModel> _copy;
        public DelegateCommand<DataGrid> InputSelectDelete => new DelegateCommand<DataGrid>(sender =>
        {
            if (_copy == null)
            {
                _copy = Core.Tool.ConversionHelper.CopyClass<SingleControlModel, SingleControlModel>();
            }
            if (sender.SelectedItems == null || sender.SelectedItems.Count == 0)
            {
                return;
            }
            List<SingleControlModel> listdata = new List<SingleControlModel>();
            foreach (SingleControlModel item in sender.SelectedItems)
            {
                SingleControlModel model = new SingleControlModel();
                _copy.Invoke(item, model);
                listdata.Add(model);
            }
            foreach (SingleControlModel item in listdata)
            {
                var model = IOP.SingleControlSource.FirstOrDefault(x => x.Name == item.Name && x.Index == item.Index);
                if (model != null)
                {
                    IOP.SingleControlSource.Remove(model);
                }
            }
            for (int i = 0; i < IOP.SingleControlSource.Count; i++)
            {
                IOP.SingleControlSource[i].Index = (ushort)(i + 1);
            }
            IOP.Save();
        });
    }
}
